package com.dzu.medium;

import java.util.HashSet;
import java.util.Set;

/**
 * @author ZhaoDong
 * @date 2023/2/2 9:41
 * @description 128. 最长连续序列
 */
public class Test128 {
    public static void main(String[] args) {
        int[] arr = new int[]{100, 4, 200, 1, 3, 2};
        System.out.println(longestConsecutive(arr));

    }

    public static int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }

        int resp = 0;
        for (int num : nums) {
            if (set.contains(num - 1)) {
                continue;
            }

            int tempNum = num;
            int temp = 1;
            while (set.contains(tempNum + 1)) {
                tempNum++;
                temp++;

            }
            resp = Math.max(resp, temp);


        }
        return resp;

    }
}
